package controllers

import (
	"B2Cshop/models"

	"github.com/astaxie/beego"
)

type GoodsControllers struct {
	beego.Controller
}

//新增商品类型
func (c *GoodsControllers) AddGoodsType() {
	parent_id, _ := c.GetInt64("parent_id")
	type_name := c.GetString("type_name")
	is_top, _ := c.GetInt("is_top")
	goods_brand_ids := c.GetString("goods_brand_ids")

	code, msg := models.AddGoodsTypeData(parent_id, type_name, is_top, goods_brand_ids)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//修改商品类型
func (c *GoodsControllers) UpdateGoodsType() {
	id, _ := c.GetInt64("id")
	parent_id, _ := c.GetInt64("parent_id")
	type_name := c.GetString("type_name")
	is_top, _ := c.GetInt("is_top")
	goods_brand_ids := c.GetString("goods_brand_ids")

	code, msg := models.UpdateGoodsTypeData(id, parent_id, type_name, is_top, goods_brand_ids)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//新增商品类型SEO
func (c *GoodsControllers) AddGoodsTypeSeo() {
	type_id, _ := c.GetInt64("type_id")
	title := c.GetString("title")
	keyword := c.GetString("keyword")
	description := c.GetString("description")

	code, msg := models.AddGoodsTypeSeoData(type_id, title, keyword, description)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//修改商品类型SEO
func (c *GoodsControllers) UpdateGoodsTypeSeo() {
	id, _ := c.GetInt64("id")
	type_id, _ := c.GetInt64("type_id")
	title := c.GetString("title")
	keyword := c.GetString("keyword")
	description := c.GetString("description")

	code, msg := models.UpdateGoodsTypeSeoData(id, type_id, title, keyword, description)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//获取商品分类列表（parent_id=0时则查询分类父级）
func (c *GoodsControllers) GoodsTypeList() {
	pagenum, _ := c.GetInt64("pagenum")
	page, _ := c.GetInt64("page")
	parent_id, _ := c.GetInt64("parent_id")

	//当前分页小于1时则默认为1
	if page < 1 {
		page = 1
	}
	code, msg, arr, pageCount := models.GetGoodsTypeList(pagenum, page, parent_id)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg, Data: arr, Page: pageCount}
	c.ServeJSON()
}

//添加商品
func (c *GoodsControllers) AddGoods() {
	goods_type_id, _ := c.GetInt64("goods_type_id")
	goods_name := c.GetString("goods_name")
	goods_introduction := c.GetString("goods_introduction")
	goods_state, _ := c.GetInt("goods_state")
	goods_stock, _ := c.GetInt64("goods_stock")
	get_integral, _ := c.GetInt64("get_integral")
	pay_type, _ := c.GetInt("pay_type")
	buy_limit, _ := c.GetInt64("buy_limit")
	sort, _ := c.GetInt("sort")
	is_hot, _ := c.GetInt("is_hot")
	goods_format := c.GetString("goods_format")

	code, msg := models.AddGoodsData(goods_type_id, goods_name, goods_introduction, goods_state, goods_stock, get_integral, pay_type, buy_limit, sort, is_hot, goods_format)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//修改商品
func (c *GoodsControllers) UpdateGoods() {
	goods_id, _ := c.GetInt64("goods_id")
	goods_type_id, _ := c.GetInt64("goods_type_id")
	goods_name := c.GetString("goods_name")
	goods_introduction := c.GetString("goods_introduction")
	goods_state, _ := c.GetInt("goods_state")
	goods_stock, _ := c.GetInt64("goods_stock")
	get_integral, _ := c.GetInt64("get_integral")
	pay_type, _ := c.GetInt("pay_type")
	buy_limit, _ := c.GetInt64("buy_limit")
	sort, _ := c.GetInt("sort")
	is_hot, _ := c.GetInt("is_hot")
	goods_format := c.GetString("goods_format")

	if goods_id < 0 {
		goods_id = 0
	}

	code, msg := models.UpdateGoodsData(goods_id, goods_type_id, goods_name, goods_introduction, goods_state, goods_stock, get_integral, pay_type, buy_limit, sort, is_hot, goods_format)
	c.Data["json"] = models.JsonBody{Code: code, Message: msg}
	c.ServeJSON()
}

//添加商品SEO
func (c *GoodsControllers) AddGoodsSeo() {
	goods_id, _ := c.GetInt64("goods_id")
	another_name := c.GetString("another_name")
	keyword := c.GetString("keyword")
	description := c.GetString("description")

	//检测该商品是否存在
	checkGoodsCode, checkGoodsMsg := models.CheckGoodsById(goods_id)
	if checkGoodsCode == models.CODE1008 {
		c.Data["json"] = models.JsonBody{Code: checkGoodsCode, Message: checkGoodsMsg}
	} else {
		//检测该商品是否存在且检测是否已添加SEO
		checkGoodsSeoCode, checkGoodsSeoMsg := models.CheckGoodsSeoById(goods_id)

		if checkGoodsSeoCode == models.CODE1009 {
			c.Data["json"] = models.JsonBody{Code: checkGoodsSeoCode, Message: checkGoodsSeoMsg}
		} else {
			code, msg := models.AddGoodsSeoData(goods_id, another_name, keyword, description)
			c.Data["json"] = models.JsonBody{Code: code, Message: msg}
		}
	}
	c.ServeJSON()
}

//修改商品SEO
// func (c *GoodsControllers) SetGoodsSeo() {
// 	id, _ := c.GetInt64("id")
// 	goods_id, _ := c.GetInt64("goods_id")
// 	another_name := c.GetString("another_name")
// 	keyword := c.GetString("keyword")
// 	description := c.GetString("description")

// 	//检测该商品是否存在
// 	checkCode, checkMsg := models.CheckGoodsById(goods_id)
// 	if checkCode == models.CODE1008 {
// 		c.Data["json"] = models.JsonBody{Code: checkCode, Message: checkMsg}
// 	} else {
// 		code, msg := models.UpdateGoodsSeo(id, goods_id, another_name, keyword, description)
// 		c.Data["json"] = models.JsonBody{Code: code, Message: msg}
// 	}
// 	c.ServeJSON()

// }
